﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FisheryPlatform.SiteConfig.FileStore;
using System.IO;

namespace FisheryPlatform.Bussiness.FileStore
{
    /// <summary>
    /// 产品静态html上传 \{yyyyMMdd}\{productid}.{ExtensionName}
    /// </summary>
    public class ProductStaticHtmlUploadProvider : FileUploadProviderBase
    {
        public static string defaultExtensionName = "product.aspx";
        public ProductStaticHtmlUploadProvider(UploadFileEntity entity)
        {
            base.Init(entity, false, FileUploadConstants.ServiceType_Product, FileUploadConstants.MimeItemKey_Attachment);
        }

        /// <summary>
        /// 文件内容
        /// </summary>
        public static string GetFileContent(DateTime uploadDate, string productId)
        {
            UploadFileEntity entity = new UploadFileEntity(productId, defaultExtensionName, new byte[0]);
            entity.UploadDate = uploadDate;
            ProductStaticHtmlUploadProvider provider = new ProductStaticHtmlUploadProvider(entity);
            return provider.Read(provider.FileServer.RootPath + entity.ServerStorePath);
        }

        /// <summary>
        /// 文件内容
        /// </summary>
        public static string GetFileContent(string publishUrlPath)
        {
            UploadFileEntity entity = new UploadFileEntity("0", defaultExtensionName, new byte[0]);
            ProductStaticHtmlUploadProvider provider = new ProductStaticHtmlUploadProvider(entity);
            return provider.Read(provider.FileServer.RootPath + publishUrlPath.Replace("/", "\\"));
        }

        /// <summary>
        /// 删除全部文件
        /// </summary>
        public static void DeleteAllFile()
        {
            UploadFileEntity entity = new UploadFileEntity("0", defaultExtensionName, new byte[0]);
            ProductStaticHtmlUploadProvider provider = new ProductStaticHtmlUploadProvider(entity);
            foreach (var dirPath in Directory.GetDirectories(provider.FileServerRootPath))
            {
                (new DirectoryInfo(dirPath)).Delete(true);
            }
        }
    }
}
